package weibo4j.crawler.test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import weibo4j.Comment;
import weibo4j.Emotion;
import weibo4j.IDs;
import weibo4j.Paging;
import weibo4j.Status;
import weibo4j.User;
import weibo4j.UserWapper;
import weibo4j.Weibo;
import weibo4j.WeiboException;

public class APITestter {

	public static void outputUsersStatus(List<User> users, OutputStream os) {
		int i = 0;
		for (User user : users) {
			System.out.println(++i + "<<-->>" + user.getName() + ":"
					+ user.getStatusesCount() + ": " + user.getCurrentStatus().getText());
		}
	}

	/**
	 * @param args
	 * @throws FileNotFoundException
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
		// TODO Auto-generated method stub
		System.out.println("Clawing...");
		try {
			if (args.length < 2) {
				System.out
						.println("You need to specify WeiboID/Password combination to show UserTimelines.");
				System.out
						.println("Usage: java weibo4j.examples.GetTimelines ID Password");
				System.exit(0);
			}

			// Other methods require authentication
			Weibo weibo = new Weibo(args[0], args[1]);

			Paging paging = new Paging(1, 200);
			
			// get the users focusing on me

//			List<User> users = weibo.getFollowersStatuses();
//			int i = 0;
//			for (User user : users) {
//				System.out.println(++i + "--->" + user.getName() + ":"
//						+ user.getDescription());
//			}

			System.out.println("++++++++++++++");
			//User u = weibo.showUserByScreenName("李开复");
			User u = weibo.showUserByUserId(1879035757L);
			System.out.println(u.getScreenName() + "--->" + u.getId() + ":"
					+ u.getDescription());
			System.out.println(u.toString());
			System.out.println(u.getUserInfoSQLString("utf-8"));
			
			
			/*
			List<Comment> comments = null;
			int page = 1;
			int i = 0;
			while(!(comments= weibo.getComments(4576278161L, paging)).isEmpty())
			{ 
				for(Comment comm : comments){
					System.out.println(++i + " " +  comm.toString());
				paging.setPage(page++);
			}
			}*/

			// get the users I focus on
			/*
			 * users = weibo.getFriendsStatuses(); i = 0; for (User user :
			 * users) { System.out .println(++i + "--->" + user.getName() + ":"
			 * + user.getDescription() + "-->>" + user.getStatusText()); }
			 */

			// get the current hot users

//			String category = "medium";
//			users = weibo.getHot(category);
//			System.out.println("-------- the current hot user on the "
//					+ category + " scene --------");
//			i = 0;
//			for (User user : users) {
//				System.out.println(++i + "--->" + user.getName() + ":"
//								+ user.getDescription() + "-->>"
//								+ user.getStatusText());
//			}

			// 返回最新n条提到登录用户的微博消息
			/*
			 * paging.setCount(2); paging.setPage(1); List<Status> statuses =
			 * weibo.getMentions(paging); i = 0; for (Status status : statuses)
			 * { System.out.println(++i + "--->" + status.getId() + ":" +
			 * status.getUser().getScreenName() + "<<-->>" + status.getText());
			 * }
			 */
			
			

			// get the specified user's friends
			String screenName = "lr001";
			long cursor = 0;
			/*
			 * IDs idss = weibo.getFriendsIDs(screenName);
			 * System.out.println(screenName + ":" + idss.getIDs().length);
			 * while (idss.hasNext()) { cursor = idss.getNextCursor(); idss =
			 * weibo.getFriendsIDs(screenName, cursor);
			 * System.out.println("cursor" + cursor + " : " +
			 * idss.getIDs().length); }
			 */

			// get the specified user's followers
			// screenName = "李开复";
			// idss = weibo.getFollowersIDs(screenName);
			// System.out.println(screenName + ":" +idss.getIDs().length);
			// while(idss.hasNext()){
			// cursor = idss.getNextCursor();
			// idss = weibo.getFollowersIDs(screenName, cursor);
			// System.out.println("cursor " + cursor + " : " +
			// idss.getIDs().length);
			// }

			// get the newly status of the specified user's followers
			/*
			screenName = "李开复";
			OutputStream os = new FileOutputStream("data.dat");
			cursor = -1;
			UserWapper uw = weibo.getFollowersStatuses(screenName, cursor);
			System.out.println(screenName + ":" + uw.getUsers().size());
			outputUsersStatus(uw.getUsers(), os);
			System.out.println(uw.getNextCursor());
			while (uw.hasNext()) {
				cursor = uw.getNextCursor();
				uw = weibo.getFollowersStatuses(screenName, cursor);
				System.out.println(screenName + ":" + uw.getUsers().size());
				outputUsersStatus(uw.getUsers(), os);
			}
			*/
			
//			List<Emotion> emotions = weibo.getEmotions();
//			System.out.println("-----Emotions List-----");			
//			for(Emotion emotion : emotions){
//				System.out.println(emotion.toString());
//			}
//			System.out.println(emotions.size());
			
			long statusId = 4576278161L;
			List<Status> statuses = null;
			Status sta = weibo.showStatus(statusId);
			System.out.println("the original status: " + sta.getUser().getName() +
					": "+ sta.getId() + ": " + sta.getCreatedAt() + ": " + sta.getText() );
			System.out.println("-----Reposting Statuses List-----");	
			paging = new Paging(1, 20);
			int page = 1;
			int i = 0;
			while(!(statuses= weibo.getRepostTimeline(statusId, paging)).isEmpty() && page == 1)
			{			
				for(Status status : statuses){
					//System.out.println(++i + "<--> " + sta.getCreatedAt() + ": " + 
					//	status.getUser().getName() + " : " + status.getText());
					//System.out.print("..");
					++i;
				}
				paging.setPage(page ++);
			}
			System.out.println("Repost count: " + i);
			
//			paging.setPage(2);
//            statuses = weibo.getRepostTimeline(statusId, paging);
//            for (Status status : statuses) {
//                System.out.println(++i + "--->" + status.getUser().getName() + ":" +
//                                   status.getText());
//            }
//            
//            paging.setPage(3);
//            statuses = weibo.getRepostTimeline(statusId, paging);
//            for (Status status : statuses) {
//                System.out.println(++i + "--->" + status.getUser().getName() + ":" +
//                                   status.getText());
//            }		
			
//			statuses = weibo.getPublicTimeline2(20L);
//			System.out.println("-----Reposting Statuses List-----");	
//			i = 0;
//			for(Status status : statuses){
//				System.out.println(++i + "<--> " + sta.getCreatedAt() + ": " + status.getUser().getName() + " : " + status.getText());
//			}
			
			System.exit(0);
		} catch (WeiboException te) {
			System.out.println("Failed to get Followers Statuses: "
					+ te.getMessage());
			System.exit(-1);
		}
	}

}
